using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AntEater.Runtime.UI;
using AntEater.ComponentModel;
using AntEater.Runtime.Services.Transformation;
using System.IO;
using System.Runtime.InteropServices;
using AntEater.Runtime.Services;

namespace AntEater.Runtime.UI.Tests
{
    /// <summary>
    /// Provides a test container for presenters.
    /// </summary>
    public partial class PresenterHostForm : Form
    {

        private const string BUILDFILEPATH = "..\\..\\TestData\\BuildFiles\\AntEater.ComponentModel.csproj.xml";
        //private const string BUILDFILEPATH = "..\\..\\TestData\\BuildFiles\\SampleBuildFile.build.xml";


        private IBuildFilePresenter _buildFilePresenter;

        /// <summary>
        /// Gets the build file presenter.
        /// </summary>
        /// <value>The build file presenter.</value>
        public IBuildFilePresenter BuildFilePresenter {
            get { return _buildFilePresenter; }
        }

        private ITargetExecutionPresenter _targetExecutionPresenter;

        /// <summary>
        /// Gets the target execution presenter.
        /// </summary>
        /// <value>The target execution presenter.</value>
        public ITargetExecutionPresenter TargetExecutionPresenter {
            get { return _targetExecutionPresenter; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:PresenterHostForm"/> class.
        /// </summary>
        public PresenterHostForm() {
            
            //Initialize Runtime Options
            AntEaterRuntime.Instance.Options.BuildEnginePath = RuntimeEnvironment.GetRuntimeDirectory();
            AntEaterRuntime.Instance.Options.ShowImportedTargets = true;

            //Load a Build File
            AntEaterRuntime.Instance.Solution.BuildFiles.Add(PresenterHostForm.GetBuildFile());
            AntEaterRuntime.Instance.Solution.Project.BuildFiles.AddRange(AntEaterRuntime.Instance.Solution.BuildFiles);

            InitializeComponent();
            InitBuildFilePresenter();
            InitTargetExecutionPresenter(); 
        }

        /// <summary>
        /// Gets the build file.
        /// </summary>
        /// <returns></returns>
        private static BuildFile GetBuildFile() {

            BuildFile buildFile = new BuildFile(PresenterHostForm.BUILDFILEPATH);

            Output output = new Output();

            //Transform Xml to ComponentModel
            IXmlToBuildElementTransformationService service = AntEaterRuntime.Instance.RuntimeServices.GetService(typeof(IXmlToBuildElementTransformationService)) as IXmlToBuildElementTransformationService;
            using (Stream buildFileXml = File.OpenRead(PresenterHostForm.BUILDFILEPATH)) {
                XmlToBuildElementTransformationResult transformationResult = service.Transform(buildFileXml, PresenterHostForm.BUILDFILEPATH);
                buildFile.Definition = transformationResult.BuildFileDefinition;
                buildFile.Messages.AddRange(transformationResult.Errors);
            }
            return buildFile;
        }

        /// <summary>
        /// Inits the build file presenter.
        /// </summary>
        private void InitBuildFilePresenter() {
           
            //Get the BuildFilePresenter
            this._buildFilePresenter = AntEaterRuntime.Instance.Presenters.GetService(typeof(IBuildFilePresenter)) as IBuildFilePresenter;
            this.BuildFilePresenter.BuildFile = AntEaterRuntime.Instance.Solution.Project.BuildFiles[0];

            //Add Surface of Presenter to TabPage
            this.buildFilePresenterTabPage.Controls.Clear();

            if (this.BuildFilePresenter != null && this.BuildFilePresenter.View != null && this.BuildFilePresenter.View.Surface != null) {
                this.buildFilePresenterTabPage.Controls.Add(this.BuildFilePresenter.View.Surface);
                this.BuildFilePresenter.View.Surface.Dock = DockStyle.Fill;
            }
        }

        /// <summary>
        /// Inits the target execution presenter.
        /// </summary>
        private void InitTargetExecutionPresenter() {

            //Get the BuildFilePresenter
            this._targetExecutionPresenter = AntEaterRuntime.Instance.Presenters.GetService(typeof(ITargetExecutionPresenter)) as ITargetExecutionPresenter;
            
            //Add Surface of Presenter to TabPage
            this.targetExecutionPresenterTabPage.Controls.Clear();

            if (this.TargetExecutionPresenter != null && this.TargetExecutionPresenter.View != null && this.TargetExecutionPresenter.View.Surface != null) {
                this.targetExecutionPresenterTabPage.Controls.Add(this.TargetExecutionPresenter.View.Surface);
                this.TargetExecutionPresenter.View.Surface.Dock = DockStyle.Fill;
            }
        }

    }
}
